package be.sc.classicnetwork.controller;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;

import be.sc.classicnetwork.model.Contact;
import be.sc.classicnetwork.model.Make;
import be.sc.classicnetwork.model.Site;

/**
 * Contact Form Controller.
 */
public class ContactFormController extends AbstractFormController {
    /**
     * @see be.sc.classicnetwork.controller.AbstractFormController#initBinder(javax.servlet.http.HttpServletRequest,
     *      org.springframework.web.bind.ServletRequestDataBinder)
     */
    @Override
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
	super.initBinder(request, binder);
    }

    /**
     * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
     */
    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
	String id = ServletRequestUtils.getStringParameter(request, "id", null);
	return (StringUtils.isEmpty(id)) ? new Contact() : getServiceLocator().getContactService().findByIdFull(id);
    }

    /**
     * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
     *      javax.servlet.http.HttpServletResponse, java.lang.Object,
     *      org.springframework.validation.BindException)
     */
    @SuppressWarnings("unchecked")
    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command,
	    BindException errors) throws Exception {
	Contact contact = (Contact) command;
	String action = ServletRequestUtils.getRequiredStringParameter(request, "action");

	// if action == delete
	if (action.equals("delete")) {
	    getServiceLocator().getContactService().delete(contact);
	    showMessage(request, "Contact successfully deleted");
	    return new ModelAndView("redirect:/private/contactForm.html").addAllObjects(referenceData(request,
		    new Contact(), null));
	}

	// if action == save
	String[] makesTokened = ServletRequestUtils.getRequiredStringParameter(request, "makesAlert").split(",");
	String[] sitesTokened = ServletRequestUtils.getRequiredStringParameter(request, "sitesAlert").split(",");

	// manage alert for makes
	contact.getMakes().clear();
	contact.getMakes().addAll(
		(Collection<? extends Make>) getServiceLocator().getCodeService().findByIdIn(Make.class, makesTokened));

	// manage no alert for site
	contact.getSites().clear();
	contact.getSites().addAll(
		(Collection<? extends Site>) getServiceLocator().getCodeService().findByIdIn(Site.class, sitesTokened));

	// save
	getServiceLocator().getContactService().saveOrUpdate(contact);
	showMessage(request, "Contact successfully saved");

	return showForm(request, errors, getFormView());
    }

    /**
     * @see org.springframework.web.servlet.mvc.SimpleFormController#referenceData(javax.servlet.http.HttpServletRequest,
     *      java.lang.Object, org.springframework.validation.Errors)
     */
    @SuppressWarnings("unchecked")
    @Override
    protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
	Contact contact = (Contact) command;
	Map data = new HashMap();

	// makes lists
	Collection makesR = getServiceLocator().getCodeService().findAll(Make.class);
	makesR.removeAll(contact.getMakes());
	data.put("makes_l", contact.getMakes());
	data.put("makes_r", makesR);

	// sites lists
	Collection sitesR = getServiceLocator().getCodeService().findAll(Site.class);
	sitesR.removeAll(contact.getSites());
	data.put("sites_l", contact.getSites());
	data.put("sites_r", sitesR);

	return data;
    }
}
